% The code is based on the following paper:
%   Chen, T. Y., Lin, Y. L., and Tzeng, L. Y., forthcoming.
%   Estimating probability weighting functions through option pricing bounds. Review of Asset Pricing Studies.
%
% Copyright: Tzu-Ying Chen, Yo-Lan Lin, Larry Y. Tzeng
% Date: January 30, 2024

clear
         
%% Setting      
BV_Target = {'UB'};  

%% Optimal Risk Preferences under Rank-Dependent Expected Utility
FileName = ['Summary_OPT_AllCR_AllPeriod_OP_UB'];
load(FileName, ...
     'Date_Monthly', ...
     'Index_CR', 'Index_OPT', 'OPT_TAB');
clear FileName 

OPT = nan(size(Date_Monthly, 1), length(Index_OPT));                       
for d = 1:size(Date_Monthly, 1)
    OPT(d, :) = OPT_TAB(Index_CR, Index_OPT, d);                                 
end 
clear Index_CR Index_OPT OPT_TAB
clear d

%% Four Specific Cases 
AllPeriod = [[19990317 19990416]; ...
	         [20030219 20030321]; ...
	         [20111019 20111118]; ...
	         [20090121 20090220]];
[~, Index_Location] = ismember(AllPeriod, Date_Monthly(:, [1 2]), 'rows');
Index_Location(Index_Location==0) = [];                                    
clear Date_Monthly

OPT = OPT(Index_Location, :);                                              
clear Index_Location

AllPeriod_Text = cell(size(AllPeriod, 1), 1);                              
for d = 1:size(AllPeriod, 1)
    if d < size(AllPeriod, 1)
        AllPeriod_Text(d, 1) = cellstr(char(['\qquad ' datestr(datenum(num2str(AllPeriod(d, 1)), 'yyyymmdd'), 'mmm dd, yyyy') ' (' num2str(25 * d) 'th percentile of $\alpha_{t}^{\ast}$)']));
    else
        AllPeriod_Text(d, 1) = cellstr(char(['\qquad \quad ' datestr(datenum(num2str(AllPeriod(d, 1)), 'yyyymmdd'), 'mmm dd, yyyy') ' (maximum of $\alpha_{t}^{\ast}$)']));
    end
end
clear d

%% Plot Figure: Probability Weighting Function
x = 0:0.001:1;

% Plot Figure
h_PWF = figure;
for d = 1:size(AllPeriod, 1)
    FileName = ['Summary_OP_Grid_' num2str(AllPeriod(d, 1))];
    load(FileName, ...
         'Record');
    clear FileName

    Index = find((Record(:, 1)==1) & ...
                 (Record(:, 2)==1));
    VIORate = 1 - Record(Index, end - 1);                                                               
    clear Index Record
    
    subplot(2, 2, d)
    plot(x, P1998(x, OPT(d, 1), OPT(d, 2)), ...
         'LineStyle', '-', ...
         'LineWidth', 2, ...
         'Color', 'r')
    hold on

    plot(x, x, ...
         'LineStyle', ':', ...
         'LineWidth', 2, ...
         'Color', 'k')
    hold on
    grid on

    h = legend(['($\alpha^\ast$ , $\beta^\ast$) = (' num2str(OPT(d, 1)) ' , ' num2str(OPT(d, 2)) ')']);
    set(h, 'FontSize', 15, ...
           'FontName', 'Times New Roman', ...
           'FontWeight', 'Bold', ...
           'Location', 'NorthWest', ...
           'Interpreter', 'Latex', ...
           'Box', 'Off')
    clear h
  
    h1 = xlabel('$p$');  
    h2 = ylabel('$v(p)$');    
    h3 = title([AllPeriod_Text(d, :); '(percentage of violations under EU = ' num2str(roundn(100 * VIORate, - 2)) '\%)']);     
    set([h1 h2 h3], 'FontSize', 15, ...
                    'FontName', 'Times New Roman', ...
                    'FontWeight', 'Bold', ...
                    'Interpreter', 'Latex')
    clear h1 h2 h3    
  
    set(gca, 'XLim', [0 1], ...
             'XTick', 0:0.25:1, ...
             'FontSize', 15, ...
             'FontName', 'Times New Roman')
    set(gca, 'YLim', [0 1], ...
             'YTick', 0:0.25:1, ...
             'FontSize', 15, ...
             'FontName', 'Times New Roman')    
    
    set(gca, 'Layer', 'Top')
    clear VIORate 
end
clear d

X = 29.7;                                                                  
Y = 21.0;                                                                  
XMargin = 1;                                                              
YMargin = 1;                                                               
XSize = X - 2 * XMargin;                                                   
YSize = Y - 2 * YMargin;                                                   
set(h_PWF, 'PaperUnits', 'Centimeters')
set(h_PWF, 'PaperSize', [X Y])
set(h_PWF, 'PaperPosition', [XMargin YMargin XSize YSize])
set(h_PWF, 'PaperOrientation', 'Portrait')
clear AllPeriod AllPeriod_Text AllCR_Target h_PWF X Y XMargin YMargin XSize YSize
clear x

%% Output
FileName = ['Figure_PWF_UB'];
saveas(gcf, [FileName '.eps'], 'epsc')
print(FileName, '-dpdf', '-fillpage')
winopen([FileName '.pdf'])
clear FileName
